package cn.itrip.biz.controller;

import cn.itrip.beans.domain.ItripUser;
import cn.itrip.beans.domain.ItripUserLinkUser;
import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.vo.itripAddUserLinkUserVO;
import cn.itrip.biz.service.HotelOrderService;
import cn.itrip.biz.service.ItripUserLinkUserService;
import cn.itrip.common.DtoUtils;
import cn.itrip.common.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/userinfo")
public class UserinfoController {
    @Resource
    private TokenUtil tokenUtil;
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;
    @RequestMapping("/queryuserlinkuser")
    public Dto queryuserlinkuser(HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if(user == null){
            return DtoUtils.returnFail("请先登录","100001");
        }
        List<ItripUserLinkUser> list = hotelOrderService.queryuserlinkuser(user.getId());
        return  DtoUtils.returnDataSuccess(list);
    }

    /**
     * 新增联系人
     * @param itripAddUserLinkUserVO
     * @param request
     * @return
     */
    @RequestMapping(value="/adduserlinkuser",method= RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto adduserlinkuser(@RequestBody itripAddUserLinkUserVO itripAddUserLinkUserVO,HttpServletRequest request){
        String token  = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if(null != user && null != itripAddUserLinkUserVO){
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkUserName(itripAddUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setLinkIdCardType(itripAddUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkIdCard(itripAddUserLinkUserVO.getLinkIdCard());
            itripUserLinkUser.setUserId(user.getId());
            itripUserLinkUser.setLinkPhone(itripAddUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setCreatedBy(user.getId());
            itripUserLinkUser.setCreationDate(new Date(System.currentTimeMillis()));
            try {
                itripUserLinkUserService.addItripUserLinkUser(itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtils.returnFail("新增常用联系人失败", "100411");
            }
            return DtoUtils.returnSuccess("新增常用联系人成功");
        }else{
            return DtoUtils.returnFail("token失效，请重新登录", "100000");
        }
    }
}
